package com.lfs.admin.controller.dataPackage;

import cn.hutool.json.JSONObject;
import com.lfs.admin.model.dto.PddProductPropertyDto;
import com.lfs.admin.model.entity.ShopEntity;
import com.lfs.admin.model.entity.dataPackage.*;
import com.lfs.admin.model.vo.*;
import com.lfs.admin.service.DataRepositoryService;
import com.lfs.admin.service.ShopService;
import com.lfs.base.dto.Result;
import com.lfs.base.dto.ResultObject;
import com.lfs.base.exception.BusinessException;
import com.lfs.common.annotation.Log;
import com.lfs.common.core.controller.BaseController;
import com.lfs.common.enums.BusinessType;
import com.lfs.dao.entity.PageBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dataRepository")
@Api(value = "数据包仓库管理", tags = {"数据包仓库管理"})
public class DataRepositoryController extends BaseController {

    @Autowired
    private DataRepositoryService dataRepositoryService;

    @Autowired
    private ShopService shopService;

    @Log(title = "数据仓库列表", businessType = BusinessType.INSERT)
    @ApiOperation(value = "数据仓库列表")
    @PostMapping("/queryRepositoryList")
    public Result<?> queryRepositoryList(@RequestBody DataRepositoryVo dataRepositoryVo) {

        if (null == dataRepositoryVo) {
            throw new BusinessException("需要查询数据仓库列表的参数为空!");
        }
        PageBean<DataRepositoryEntity> dataRepositoryList = dataRepositoryService.queryDataRepositoryList(dataRepositoryVo);
        return ResultObject.successObject(dataRepositoryList,"success");

//        logger.info("根据{},查询产品列表请求参数", productInfoVo.toString());
//        return AjaxResult.error("文件上传后解析失败!");
    }

    @Log(title = "查看调整产品数据", businessType = BusinessType.INSERT)
    @ApiOperation(value = "查看调整产品数据")
    @GetMapping("/getUploadDetail")
    public Result<?> getUploadDetail(@RequestParam("id") int id,
                                       @RequestParam("currentPage") int currentPage,
                                       @RequestParam("pageSize") int pageSize) {
        PageBean<JSONObject> dataRepositoryList = dataRepositoryService.getUploadDetail(id,currentPage,pageSize);
        return ResultObject.successObject(dataRepositoryList,"success");

//        logger.info("根据{},查询产品列表请求参数", productInfoVo.toString());
//        return AjaxResult.error("文件上传后解析失败!");
    }


    @ApiOperation(value = "删除数据仓库记录")
    @DeleteMapping("/deleteRepository")
    public Result<?> deleteRepository(@RequestParam("id") Integer id) {

        if (null == id) {
            throw new BusinessException("删除数据仓库记录接口参数不能为空!");
        }
        logger.info("根据{},删除数据仓库记录请求参数", id);
        return ResultObject.successObject(dataRepositoryService.deleteDataRepository(id), "删除数据包记录成功");
    }

    @ApiOperation(value = "修改数据包仓库")
    @PostMapping("/updateRepository")
    public Result<?> updateRepository(@RequestBody DataRepositoryEntity entity) {
        if (entity == null) {
            throw new BusinessException("数据包仓库参数不能为空!");
        }
        dataRepositoryService.updateRepository(entity);
        return ResultObject.successMessage("success");
    }

    // -----------------------------------发布产品（开始）--------------------------------------
    // @ApiOperation(value = "根据名称商品类目信息")
    // @PostMapping("/ListProductTypeByName")
    // public Result<?> ListProductTypeByName(@RequestParam("ids") List<Integer> ids) {
    //     List<ProductTypeEntity> productTypes = dataRepositoryService.productTypeList(ids);
    //     return ResultObject.successObject(productTypes,"success");
    // }


    @ApiOperation(value = "获取商品类目信息")
    @PostMapping("/getCurrentCommodityCategory")
    public Result<?> getCurrentCommodityCategory(@RequestParam("name") String name, @RequestParam("shopId") String shopId) {
        PddGoodsAuthVO category = dataRepositoryService.getCurrentCommodityCategory(name, shopId);
        return ResultObject.successObject(category,"success");
    }

    @ApiOperation(value = "查询当前用户下可用的店铺列表")
    @PostMapping("/getCurrUserShopAllData")
    public Result<?> getCurrUserShopAllData() {
        List<ShopEntity> shopEntityList = shopService.getCurrUserShopAllData();
        return ResultObject.successObject(shopEntityList,"success");
    }

    @ApiOperation(value = "查询选择的店铺信息")
    @PostMapping("/queryShopListById")
    public Result<?> queryShopListById(@RequestParam("shopIds") List<String> shopIds) {
        List<ShopVo> shopEntityList = dataRepositoryService.queryShopListById(shopIds);
        return ResultObject.successObject(shopEntityList,"success");
    }

    @ApiOperation(value = "查询品牌")
    @PostMapping("/searchPddBrand")
    public Result<?> searchPddBrand(@RequestParam("value") String value, @RequestParam("catId") Long catId, @RequestParam("shopId")String shopId) {
        List<PddGoodsPropertiesVO>  pddGoodsPropertiesVOS = dataRepositoryService.searchPddBrand(value, catId, shopId);
        return ResultObject.successObject(pddGoodsPropertiesVOS,"success");
    }

    @ApiOperation(value = "同步数据")
    @PostMapping("/synchronousData")
    public Result<?> synchronousData(@RequestParam("shopId") String shopId) {
        ShopVo shopVo = dataRepositoryService.synchronousData(shopId);
        return ResultObject.successObject(shopVo, "success");
    }

    @ApiOperation(value = "商品参数模板")
    @PostMapping("/productPropertyTemplateList")
    public Result<?> productPropertyTemplateList(@RequestParam("shopId") String shopId) {
        List<ProductPropertyTemplateEntity> templateEntityList = dataRepositoryService.productPropertyTemplateList(shopId);
        return ResultObject.successObject(templateEntityList, "success");
    }
    @ApiOperation(value = "根据ID获取商品参数模板")
    @GetMapping("/getProductPropertyTempById")
    public Result<?> getProductPropertyTempById(@RequestParam("id") Integer id) {
        ProductPropertyTemplateEntity templateEntity = dataRepositoryService.getProductPropertyTempById(id);
        return ResultObject.successObject(templateEntity, "success");
    }
    @ApiOperation(value = "根据ID删除商品参数模板")
    @DeleteMapping("/deleteProductPropertyTempById")
    public Result<?> deleteProductPropertyTempById(@RequestParam("id") Integer id) {
        dataRepositoryService.deleteProductPropertyTempById(id);
        return ResultObject.successMessage("success");
    }

    @ApiOperation(value = "更新或新增商品参数模板")
    @PostMapping("/updateOrAddProductPropertyTemp")
    public Result<?> updateOrAddProductPropertyTemp(@RequestBody ProductPropertyTemplateEntity productPropertyTemplate) {
        dataRepositoryService.updateOrAddProductPropertyTemp(productPropertyTemplate);
        return ResultObject.successMessage("success");
    }
    @ApiOperation(value = "查询商品款式、风格、保护套质地、流行元素、自定义规格通用接口")
    @PostMapping("/getNodeData")
    public Result<?> getNodeData(@RequestParam("catPropId") String catPropId, String shopId) {
        List<PddGoodsPropertiesVO> serviceNodeData = dataRepositoryService.getNodeData(catPropId, shopId);
        return ResultObject.successObject(serviceNodeData, "success");
    }

    @ApiOperation(value = "获取自动标题模板")
    @PostMapping("/getTitleTemplates")
    public Result<?> getTitleTemplates() {
        List<TitleTemplateEntity> templateList = dataRepositoryService.getTitleTemplates();
        return ResultObject.successObject(templateList, "success");
    }

    @ApiOperation(value = "根据ID获取自动标题模板")
    @GetMapping("/getTitleTemplatesById")
    public Result<?> getTitleTemplatesById(@RequestParam("id") Integer id) {
        TitleTemplateEntity template = dataRepositoryService.getTitleTemplatesById(id);
        return ResultObject.successObject(template, "success");
    }

    @ApiOperation(value = "更新或新增自动标题模板")
    @PostMapping("/updateOrAddTitleTemplate")
    public Result<?> updateOrAddTitleTemplate(@RequestBody TitleTemplateEntity titleTemplate) {
        dataRepositoryService.updateOrAddTitleTemplate(titleTemplate);
        return ResultObject.successMessage("success");
    }

    @ApiOperation(value = "根据ID删除自动标题模板")
    @DeleteMapping("/deleteTitleTemplateById")
    public Result<?> deleteTitleTemplateById(@RequestParam("id") Integer id) {
        dataRepositoryService.deleteTitleTemplateById(id);
        return ResultObject.successMessage("success");
    }

    @ApiOperation(value = "获取默认标题")
    @PostMapping("/getDefaultTitle")
    public Result<?> getDefaultTitle(@RequestParam("ids") List<Integer> ids) {
        List<DataUploadDetailVO> resultVO = dataRepositoryService.getDefaultTitle(ids);
        return ResultObject.successObject(resultVO, "success");
    }

    // @ApiOperation(value = "获取自动标题")
    // @PostMapping("/getSpontaneousTitle")
    // public Result<?> getSpontaneousTitle(@RequestBody SpontaneousTitleDto spontaneousTitleDto) {
    //     List<DataUploadImageVO> resultVO = dataRepositoryService.getSpontaneousTitle(spontaneousTitleDto);
    //     return ResultObject.successObject(resultVO, "success");
    // }


    @ApiOperation(value = "获取词库")
    @PostMapping("/gainWord")
    public Result<?> gainWord(@RequestParam("titleTypeEnum") String titleTypeEnum) {
        TitleTypeVO titleTypeVO = dataRepositoryService.gainWord(titleTypeEnum);
        return ResultObject.successObject(titleTypeVO, "success");
    }

    @ApiOperation(value = "增加词库")
    @PostMapping("/addGainWord")
    public Result<?> addGainWord(@RequestBody List<TitleWordEntity> titleWordEntity) {
        dataRepositoryService.addGainWord(titleWordEntity);
        return ResultObject.successMessage("success");
    }


    @ApiOperation(value = "发布商品")
    @PostMapping("/releaseGoods")
    public Result<?> releaseGoods(@RequestBody @Validated PddProductPropertyDto pddProductPropertyDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultObject.warnMessage("参数校验失败: " + bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        List<Map<String, Object>> releaseGoods = dataRepositoryService.releaseGoods(pddProductPropertyDto);
        return ResultObject.successObject(releaseGoods, "success");
    }

    @ApiOperation(value = "批量发布商品")
    @PostMapping("/batchReleaseGoods")
    public Result<?> batchReleaseGoods(@RequestBody List<PddProductPropertyDto> pddProductPropertyDto) {
        List<List<Map<String, Object>>> releaseGoods = dataRepositoryService.batchReleaseGoods(pddProductPropertyDto);
        return ResultObject.successObject(releaseGoods, "success");
    }


    @ApiOperation(value = "导出将要发布商品数据")
    @RequestMapping("/exportWillReleaseExcel")
    public Result<?> exportWillReleaseExcel(HttpServletResponse response, String seriesName, Integer repositoryId) {
        dataRepositoryService.exportWillReleaseExcel(response, seriesName, repositoryId);
        return ResultObject.successMessage("success");
    }

    @ApiOperation(value = "导入发布商品数据")
    @RequestMapping("/importWillReleaseExcel")
    public Result<?> importWillReleaseExcel(@RequestPart("pddProductPropertyDto") @Validated PddProductPropertyDto pddProductPropertyDto, @RequestParam("file") MultipartFile file) {
        dataRepositoryService.importWillReleaseExcel(pddProductPropertyDto, file);
        return ResultObject.successMessage("success");
    }

    // -----------------------------------发布产品（结束）--------------------------------------

    // -----------------------------------查看/调整产品数据--------------------------------------
    @ApiOperation(value = "查询产品数据列表")
    @PostMapping("/queryProductRecordList")
    public Result<?> queryProductRecordList(@RequestBody DataUploadDetailVO dataUploadDetailVO) {
        if (null == dataUploadDetailVO) {
            throw new BusinessException("需要查询产品数据列表的参数为空!");
        }
        PageBean<DataUploadDetailVO> queryProductRecordList = dataRepositoryService.queryProductRecordList(dataUploadDetailVO);
        return ResultObject.successObject(queryProductRecordList,"success");
    }

    @ApiOperation(value = "修改产品名称")
    @PostMapping("/updateProductRecord")
    public Result<?> updateProductRecord(@RequestBody DataUploadDetailVO dataUploadDetailVO) {
        dataRepositoryService.updateProductRecord(dataUploadDetailVO);
        return ResultObject.successMessage("success");
    }

    @ApiOperation(value = "根据ID删除产品数据")
    @DeleteMapping("/deleteProductRecordById")
    public Result<?> deleteProductRecordById(@RequestParam("id") Integer id) {
        dataRepositoryService.deleteProductRecordById(id);
        return ResultObject.successMessage("success");
    }

    @ApiOperation(value = "预发布查看")
    @PostMapping("/getProductGoodsData")
    public Result<?> getProductGoodsData(@RequestParam("id") Integer id) {
        ProductGoodsDataVO productGoodsData = dataRepositoryService.getProductGoodsData(id);
        return ResultObject.successObject(productGoodsData,"success");
    }



    // -----------------------------------更多操作--------------------------------------
    @ApiOperation(value = "延长失效时间")
    @GetMapping("/prolongExpireTime")
    public Result<?> prolongExpireTime(@RequestParam("id") Integer id) {
        dataRepositoryService.prolongExpireTime(id);
        return ResultObject.successMessage("success");
    }

    @ApiOperation(value = "生成副本")
    @GetMapping("/copySeries")
    public Result<?> copySeries(@RequestParam("id") Integer id, @RequestParam("count") Integer count) {
        if (count < 1) {
            throw new BusinessException("至少生成1个副本!");
        }
        dataRepositoryService.copySeries(id, count);
        return ResultObject.successMessage("success");
    }
}
